﻿using BusinessCache;
using BusinessDB;
using BusinessService.proto;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TemplateGenerator;

namespace BusinessService
{
    public class SearchBusiness
    {
        private IList<ProdDB> GetProd()
        {
            ProdService ps = new ProdService();
            return ps.GetItems();
        }

        public IEnumerable<ProdDB> InnerSearchProdClass(byte[] conditions)
        {
            CondProdClass cond = CondProdClass.ParseFrom(conditions);

            IList<ProdDB> list = GetProd();
            IEnumerable<ProdDB> query = null;
            if (cond.HasClass)
            {
                query = from r in list
                        where r.ProdClass == cond.Class
                        select r;
            }

            if (cond.HasSetup)
            {
                query = from r in list
                        where r.ProjSetup == cond.Setup
                        select r;
            }

            if (cond.HasUsePlace)
            {
                query = from r in list
                        where r.ProdPlace == cond.UsePlace
                        select r;
            }
            return query;
        }

        // 二级页面
        public byte[] SearchProdClass(byte[] conditions)
        {
            ProdClassArray.Builder builder = new ProdClassArray.Builder();
            ProdConverter converter = new ProdConverter();
            foreach (ProdDB prod in InnerSearchProdClass(conditions))
            {
                builder.ProdClassList.Add(converter.ToClass(prod));
            }

            return builder.Build().ToByteArray();
        }

        public IEnumerable<ProdDB> InnerSearchProdSeries(byte[] conditions)
        {
            CondProdSeries cond = CondProdSeries.ParseFrom(conditions);

            IList<ProdDB> list = GetProd();
            IEnumerable<ProdDB> query = null;

            if (cond.HasProductClass)
            {
                query = from r in list
                        where r.ProdClass == cond.ProductClass
                        select r;
            }

            if (cond.HasUsePlace)
            {
                query = from r in list
                        where r.ProdPlace == cond.UsePlace
                        select r;
            }

            if (cond.HasLable)
            {
                query = from r in list
                        where r.ProdLabel == cond.Lable
                        select r;
            }

            if (cond.HasSeries)
            {
                query = from r in list
                        where r.ProdSeries == cond.Series
                        select r;
            }

            if (cond.HasAdpaterClass)
            {
                query = from r in list
                        where r.AdpClass == cond.AdpaterClass
                        select r;
            }

            if (cond.HasSetup)
            {
                query = from r in list
                        where r.ProjSetup == cond.Setup
                        select r;
            }
            return query;
        }

        // 三级页面
        public byte[] SearchProdSeries(byte[] conditions)
        {
            ProdViewArray.Builder builder = new ProdViewArray.Builder();
            ProdConverter converter = new ProdConverter();
            foreach (ProdDB prod in InnerSearchProdSeries(conditions))
            {
                builder.ViewList.Add(converter.ToView(prod));
            }

            return builder.Build().ToByteArray();
        }

        public string SearchProdSeriesToPDF(byte[] conditions)
        {
            ListGenerator generator = new ListGenerator();
            return generator.Generate(InnerSearchProdSeries(conditions), true);
        }

        public string SearchProdSeriesToExcel(byte[] conditions)
        {
            ListGenerator generator = new ListGenerator();
            return generator.Generate(InnerSearchProdSeries(conditions));
        }

        public IEnumerable<ProdDB> InnerSearchProdList(byte[] conditions)
        {
            CondPordList cond = CondPordList.ParseFrom(conditions);
            IList<ProdDB> list = GetProd();

            IEnumerable<ProdDB> query = null;
            if (cond.HasSeries)
            {
                query = from r in list
                        where r.ProdSeries == cond.Series
                        select r;
            }

            if (cond.HasUsePlace)
            {
                query = from r in list
                        where r.ProdPlace == cond.UsePlace
                        select r;
            }

            if (cond.HasProductClass)
            {
                query = from r in list
                        where r.ProdClass == cond.ProductClass
                        select r;
            }

            if (cond.HasProductVersion)
            {
                query = from r in list
                        where r.ProdVersion == cond.ProductVersion
                        select r;
            }

            if (cond.HasProductLable)
            {
                query = from r in list
                        where r.ProdLabel == cond.ProductLable
                        select r;
            }

            if (cond.HasStartLightingPower && cond.HasEndLightingPower)
            {
                query = from r in list
                        where r.EleLgtPower >= cond.StartLightingPower && r.EleLgtPower <= cond.EndLightingPower
                        select r;
            }
            else if (cond.HasStartLightingPower)
            {
                query = from r in list
                        where r.EleLgtPower >= cond.StartLightingPower
                        select r;
            }
            else if (cond.HasEndLightingPower)
            {
                query = from r in list
                        where r.EleLgtPower <= cond.EndLightingPower
                        select r;
            }

            //4.	光源色温下拉菜单分为三档暖<3300K
            //中性白 3300K-5000K
            //冷>5000K
            if (cond.HasColor)
            {
                switch (cond.Color)
                {
                    case 1:
                        query = from r in list
                                where r.AdpColor > 0 && r.AdpColor < 3300
                                select r;
                        break;
                    case 2:
                        query = from r in list
                                where r.AdpColor >= 3300 && r.AdpColor <= 5000
                                select r;
                        break;
                    case 3:
                        query = from r in list
                                where r.AdpColor > 5000
                                select r;
                        break;
                }
            }

            if (cond.HasCri)
            {
                switch (cond.Cri)
                {
                    case 1:
                        query = from r in list
                                where r.AdpCri > 0 && r.AdpCri < 79
                                select r;
                        break;
                    case 2:
                        query = from r in list
                                where r.AdpCri >= 80 && r.AdpCri <= 89
                                select r;
                        break;
                    case 3:
                        query = from r in list
                                where r.AdpCri > 90
                                select r;
                        break;
                }
            }

            if (cond.HasStartMarketPrice && cond.HasEndMarketPrice)
            {
                query = from r in list
                        where r.BusMarket >= cond.StartMarketPrice && r.BusMarket <= cond.EndMarketPrice
                        select r;
            }
            else if (cond.HasStartMarketPrice)
            {
                query = from r in list
                        where r.BusMarket >= cond.StartMarketPrice
                        select r;
            }
            else if (cond.HasEndMarketPrice)
            {
                query = from r in list
                        where r.BusMarket <= cond.EndMarketPrice
                        select r;
            }
            return query;
        }

        // 三级搜索界面
        public byte[] SearchProdList(byte[] conditions)
        {
            ProdViewArray.Builder builder = new ProdViewArray.Builder();
            ProdConverter converter = new ProdConverter();
            foreach (ProdDB prod in InnerSearchProdList(conditions))
            {
                builder.ViewList.Add(converter.ToView(prod));
            }

            return builder.Build().ToByteArray();
        }

        public string SearchProdListToPDF(byte[] conditions)
        {
            ListGenerator generator = new ListGenerator();
            return generator.Generate(InnerSearchProdList(conditions), true);
        }

        public string SearchProdListToExcel(byte[] conditions)
        {
            ListGenerator generator = new ListGenerator();
            return generator.Generate(InnerSearchProdList(conditions));
        }

        public ProdDB GetProd(int id)
        {
            return (from r in GetProd()
                    where r.Id.Pid == id
                    select r).FirstOrDefault();
        }

        // 四级页面
        public byte[] SearchPrimaryProdInfo(short id)
        {
            var item = (from r in GetProd()
                        where r.Id.Pid == id
                        select r).FirstOrDefault();
            if (item != null)
            {
                ProdConverter converter = new ProdConverter();
                return converter.ToPrimaryProd(item).ToByteArray();
            }
            return null;
        }

        public string SearchPrimaryProdInfoToPDF(int id)
        {
            SpecGenerator generator = new SpecGenerator();
            return generator.Generate(GetProd(id), true);
        }

        public string SearchPrimaryProdInfoToExcel(int id)
        {
            SpecGenerator generator = new SpecGenerator();
            return generator.Generate(GetProd(id));
        }


        // 五级页面
        public byte[] SearchProdInfo(short id)
        {
            var item = (from r in GetProd()
                        where r.Id.Pid == id
                        select r).FirstOrDefault();

            if (item != null)
            {
                ProdConverter converter = new ProdConverter();
                return converter.ToProd(item).ToByteArray();
            }

            return null;
        }

        public string SearchProdInfoToPDF(int id)
        {
            SpecAllGenerator generator = new SpecAllGenerator();
            return generator.Generate(GetProd(id), true);
        }

        public string SearchProdInfoToExcel(int id)
        {
            SpecAllGenerator generator = new SpecAllGenerator();
            return generator.Generate(GetProd(id));
        }

        // 推荐商品
        public byte[] SearchRecommendedProd(byte[] conditions)
        {
            throw new NotImplementedException();
        }
    }
}
